class CommentsController < ApplicationController
  
  layout "uniblog"
  before_filter :verify_access, :except => [:index, :show, :new, :create]
  
  # GET /comments
  # GET /comments.xml
  def index
    entry = Entry.find(params[:entry_id])
    @comments = entry.comments.find(:all)
  
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @comments.to_xml }
    end
  end

  # GET /comments/1
  # GET /comments/1.xml
  def show
    @comment = Comment.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @comment.to_xml }
    end
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1;edit
  def edit
    @comment = Comment.find(params[:id])
  end

  # POST /comments
  # POST /comments.xml
  def create
    @comment = Comment.new(params[:comment])
    @comment.entry = Entry.find(params[:entry_id])
    respond_to do |format|
      if @comment.save
        flash[:notice] = 'Comment was successfully created.'
        format.html { redirect_to comment_url(@comment.entry, @comment) }
        format.xml  { head :created, :location => comment_url(@comment.entry, @comment) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @comment.errors.to_xml }
      end
    end
  end

  # PUT /comments/1
  # PUT /comments/1.xml
  def update
    @comment = Comment.find(params[:id])

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        flash[:notice] = 'Comment was successfully updated.'
        format.html { redirect_to comment_url(@comment.entry_id, @comment) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comment.errors.to_xml }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.xml
  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to comments_url(@comment.entry_id) }
      format.xml  { head :ok }
    end
  end
end
